home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Float2Str255 / Float2Str255.c
Encoding:
C/C++ Source or Header  |  1996-01-03  |  934 b   |  23 lines  |  [TEXT/R*ch]

  1. /* Float2Str255.c    by Stefan Sinclair    Oct. 1995
  2.     Apple provided a routine for converting integers (type 'long') to
  3.     into Str255 format (NumToString), but they forgot floating point conversion!
  4.     This simple routine converts from floating point (type 'double')
  5.     into Str255 format.
  6. */
  7.  
  8. #include <stdio.h> /* You need this one */
  9. /* #include <Macheaders>   <- You will need to un-comment this line.
  10.  Also, p2cstr and c2pstr are now defined in Apple's Macintosh headers.
  11.  */
  12.  
  13. void Float2Str255(double fPt, Str255 pStr)
  14. {
  15.     int    myErr;
  16.     
  17.     p2cstr(pStr); /* Convert to (char *) */
  18.     myErr = sprintf((char *)pStr, "%G", fPt); /* print floating point number */
  19.     /* You can easily change the output format by replacing '%G' with whatever makes you happy. */
  20.     if(myErr >= 0) /* Did everything go as planned? */
  21.         c2pstr((char *)pStr); /* Back to Str255 */
  22.     /* else, here you will want to handle any error that might occur for some freak reason. */
  23. }